home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Arsenal Files 6
/
The Arsenal Files 6 (Arsenal Computer).ISO
/
prg_casm
/
jlvesa11.zip
/
JVEXMPLE.CPP
< prev
next >
Wrap
Text File
|
1996-01-03
|
11KB
|
433 lines
/************************************************************************
* This file is part of JLVesa SVGA library v1.0
* Copyright 1995, 1996 Johannes Lehtinen
* All rights reserved
************************************************************************/
// All functions used should be ANSI C compatible
#include <stdio.h>
#include <stdlib.h>
#include "jlvesa.h"
// Define image size for demos
#define IMAGE_SIZE 100
// Function prototypes
void Demo_SinglePage(void);
void Demo_MultiPage(void);
void Error_Fatal(char *msg);
// Graphics figure used in demo
unsigned char jl_figure[]={
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\000\000\017\017\017\017\017\017\000\000\000\000\000\000\000\000\000"
"\000\000\000\017\017\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\017\017\000\000\000\000\000\000\000\000\000\000\000\000\000\000\017"
"\017\000\000\000\000\000\000\000\000\000\000\000\000\000\000\017\017\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\017\017\000\000\017"
"\000\000\000\000\000\000\000\000\000\000\000\017\017\000\000\017\000\000"
"\000\000\000\000\000\000\000\000\000\017\017\000\000\017\000\000\000\000"
"\000\000\000\000\000\000\000\017\017\000\000\017\000\000\000\000\000\017"
"\017\000\000\000\000\017\017\000\000\017\000\000\000\000\000\017\017\000"
"\000\000\000\017\017\000\000\017\000\000\000\000\000\017\017\017\000\000"
"\017\017\017\000\000\017\017\017\017\000\000\000\017\017\017\017\017\017"
"\000\000\000\000\000\000\000\000\000\000\000\017\017\017\017\000\000\000"
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
"\000\000\000\000"};
// Main function
void main(void)
{
JVSVGAInfo vgainfo;
// Get some information about the video card
if(JVSVGA_GetInfo(&vgainfo)!=JV_SUCCESS)
{
// No VESA found
printf("error: VESA interface not found!\n"
"Your video card is not VESA compatible. Try UNIVBE driver!\n");
exit(1);
}
// Try to switch to 640x480/256 mode
if(JVSVGA_SetMode(0x101)!=0)
{
// Failure
printf("error: Can't set 640x480/256 mode\n"
"Your video card may not be VESA compatible or it may not\n"
"support the mode.\n");
exit(1);
}
// Do single page demo
Demo_SinglePage();
// Do multiple page demo if possible
if(vgainfo.memory>=16)
Demo_MultiPage();
// Return to 80x25 mode
JVBIOS_SetMode(3);
// Give user some information about program
printf("JVExample v1.0\n"
"Copyright 1995, 1996 Johannes Lehtinen\n"
"All rights reserved\n\n"
"This program is a simple example of how to use JLVesa SVGA library.\n"
"If graphics didn't seem to work OK, try loading UNIVBE driver before\n"
"executing this program.\n");
exit(0);
}
// Single page demo
void Demo_SinglePage(void)
{
int i, n, a;
int color;
JVPalette palette;
JVPalette palette2;
void *image, *background;
int imgx, imgy;
// Set black-red-purple-green-black palette
for(color=0; color<256; color++)
{
if(color<64) // Colors 0-63 (black - red)
{
palette[color][0]=color;
palette[color][1]=0;
palette[color][2]=0;
}
else if(color<128) // Colors 64-127 (red-yellow)
{
palette[color][0]=63;
palette[color][1]=0;
palette[color][2]=color-64;
}
else if(color<192) // Colors 128-191 (yellow-green)
{
palette[color][0]=63-(color-128);
palette[color][1]=color-128;
palette[color][2]=63-(color-128);
}
else // Colors 192-255 (green-black)
{
palette[color][0]=0;
palette[color][1]=63-(color-192);
palette[color][2]=0;
}
}
JVPalette_Set(palette);
// Draw some simple shapes to screen
color=0;
for(i=0; i<320; i++)
{
JVLine_Draw(319, 0, i, 479, color);
JVLine_Draw(320, 0, 639-i, 479, color);
JVLine_Draw(0, 479, 319-i, 0, color);
JVLine_Draw(639, 479, 320+i, 0, color);
if(++color>255)
color=0;
}
// Scroll palette round
for(i=0; i<512; i++)
{
// Create new palette
for(n=0; n<256; n++)
{
color=(i+n) % 256;
for(a=0; a<3; a++)
palette2[color][a]=palette[n][a];
}
// Wait for vertical retrace and set palette
JVSVGA_WaitForVRetrace();
JVPalette_Set(palette2);
}
// Demonstrate horizontal scrolling
for(i=0; i<640; i+=4)
{
JVSVGA_WaitForVRetrace();
JVScreen_SetVisual(i, 0);
}
for(i=639; i>0; i-=4)
{
JVSVGA_WaitForVRetrace();
JVScreen_SetVisual(i, 0);
}
// Demonstrate vertical scrolling
for(i=0; i<480; i+=4)
{
JVSVGA_WaitForVRetrace();
JVScreen_SetVisual(0, i);
}
for(i=479; i>0; i-=4)
{
JVSVGA_WaitForVRetrace();
JVScreen_SetVisual(0, i);
}
JVSVGA_WaitForVRetrace();
JVScreen_SetVisual(0, 0);
// Demonstrate image read/draw operations
// Allocate memory for image and its background
if((image=malloc(IMAGE_SIZE*IMAGE_SIZE))==NULL ||
(background=malloc(IMAGE_SIZE*IMAGE_SIZE))==NULL)
Error_Fatal("Memory allocation error");
// Read image from upper right corner
JVImage_Read(540, 0, IMAGE_SIZE, IMAGE_SIZE, image);
JVImage_Read(540, 0, IMAGE_SIZE, IMAGE_SIZE, background);
imgx=540;
imgy=0;
// Move image around screen like sprite
for(i=540; i>=0; i-=8)
{
JVSVGA_WaitForVRetrace();
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
imgx=i;
JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
JVSVGA_WaitForVRetrace();
}
for(i=0; i<380; i+=8)
{
JVSVGA_WaitForVRetrace();
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
imgy=i;
JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
JVSVGA_WaitForVRetrace();
}
for(i=0; i<540; i+=8)
{
JVSVGA_WaitForVRetrace();
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
imgx=i;
JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
JVSVGA_WaitForVRetrace();
}
for(i=380; i>=0; i-=8)
{
JVSVGA_WaitForVRetrace();
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
imgy=i;
JVImage_Read(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, image);
JVSVGA_WaitForVRetrace();
}
JVImage_Draw(imgx, imgy, IMAGE_SIZE, IMAGE_SIZE, background);
free(image);
free(background);
// Fade out palette
for(i=128; i>=0; i--)
{
// Create new palette
for(color=0; color<256; color++)
for(n=0; n<3; n++)
palette2[color][n]=(palette[color][n]*i)/128;
// Set palette
JVSVGA_WaitForVRetrace();
JVPalette_Set(palette2);
}
}
// Multiple video page demo
void Demo_MultiPage(void)
{
int i, n, color, x, y;
int xf, yf;
int fgcolor, bgcolor;
JVUByte *tile, *image, *background;
JVPalette palette, palette2;
int pos[2]={0,0}, speed[2]={6, 5};
// Try to set logical width to 1024
if(JVScreen_SetLogicalWidth(1024)!=1024)
Error_Fatal("Can not set logical width to 1024");
// Allocate memory for tiles and images
if((tile=(char *)malloc(128*128))==NULL ||
(image=(char *)malloc(64*64))==NULL ||
(background=(char *)malloc(64*64))==NULL)
Error_Fatal("Memory allocation error");
// Create bouncing image
JVRectangle_Draw(0, 0, 64, 64, 0);
for(i=0; i<16; i++)
{
color=255-i;
JVLine_Draw(16-i, i, 47+i, i, color);
JVLine_Draw(16-i, 63-i, 47+i, 63-i, color);
JVLine_Draw(i, 16, i, 47, color);
JVLine_Draw(63-i, 16, 63-i, 47, color);
}
JVImage_Read(0, 0, 64, 64, image);
// Create background
for(y=0; y<8; y++)
for(x=0; x<8; x++)
{
for(i=0; i<8; i++)
for(n=0; n<8; n++)
{
// Choose colors for this figure
fgcolor=y | (x<<3) | (((i+n)%4) << 6);
bgcolor=fgcolor ^ 0xff;
// Put figure to tile
for(yf=0; yf<16; yf++)
for(xf=0; xf<16; xf++)
if(jl_figure[yf*16+xf]!=0)
tile[(i*16+yf)*128+n*16+xf]=fgcolor;
else
tile[(i*16+yf)*128+n*16+xf]=bgcolor;
}
// Put tile to screen
JVImage_DrawLimited(x*128, y*128, 128, 128, tile, 0, 0, 1024, 1024);
}
// Calculate palette
for(color=0; color<256; color++)
{
palette[color][0]=(color & 3) * (color>>6) * 7;
palette[color][1]=((color>>2)&3) * (color>>6) * 7;
palette[color][2]=((color>>4)&3) * (color>>6) * 7;
}
// Fade in palette
for(i=0; i<=128; i++)
{
// Create palette
for(color=0; color<256; color++)
for(n=0; n<3; n++)
palette2[color][n]=(palette[color][n]*i)/128;
// Set palette
JVSVGA_WaitForVRetrace();
JVPalette_Set(palette2);
}
// Bounce image some time
JVImage_Read(pos[0], pos[1], 64, 64, background);
for(n=0; n<2000; n++)
{
// Wait for vertical retrace
JVSVGA_WaitForVRetrace();
// Wipe off old image
JVImage_DrawLimited(pos[0], pos[1], 64, 64, background, 0, 0, 1024, 1024);
// Calculate new position
for(i=0; i<2; i++)
{
pos[i]+=speed[i];
if(pos[i]<0) {
pos[i]=0;
speed[i]=-speed[i]; }
if(pos[i]>1024-64) {
pos[i]=1024-64;
speed[i]=-speed[i]; }
}
// Reposition screen as necessary
x=pos[0]-320+32;
if(x<0) x=0;
if(x>1024-640) x=1024-640;
y=pos[1]-240+32;
if(y<0) y=0;
if(y>1024-480) y=1024-480;
JVScreen_SetVisual(x, y);
// Read background and place new image
JVScreen_SetActive((long)pos[1]*1024L+(long)pos[0]);
JVImage_Read(0, 0, 64, 64, background);
JVImage_DrawOnLimited(0, 0, 64, 64, image, 0, 0, 1024, 1024);
JVScreen_SetActive(0);
}
// Fade out palette
for(i=128; i>=0; i--)
{
// Create new palette
for(color=0; color<256; color++)
for(n=0; n<3; n++)
palette2[color][n]=(palette[color][n]*i)/128;
// Set palette
JVSVGA_WaitForVRetrace();
JVPalette_Set(palette2);
}
return;
}
// Fatal error handler
void Error_Fatal(char *msg)
{
JVBIOS_SetMode(3);
fprintf(stderr, "error: %s\n", msg);
exit(1);
}